<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var minCharacters = function (a, b) {
      let da = new Array(26).fill(0);
      let db = new Array(26).fill(0);
      for (let i in a) {
        da[a.charCodeAt(i) - 97]++;
      }
      for (let i in b) {
        db[b.charCodeAt(i) - 97]++;
      }
      let an = a.length, bn = b.length, asum = 0, bsum = 0, res = Number.MAX_SAFE_INTEGER;
      for (let i = 0; i < 25; i++) {
        // 前缀和计算
        asum += da[i];
        bsum += db[i];
        // 找最小值
        res = Math.min(res, an + bn - da[i] - db[i], an - asum + bsum, bn - bsum + asum);
      }
      // z的特殊处理
      return Math.min(res, an + bn - da[25] - db[25]);
    };
  </script>


</body>

</html>